# -*- coding: utf-8 -*-
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
from forms import *
from django.shortcuts import redirect,get_object_or_404
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
from django.contrib.auth.decorators import login_required

@login_required
def create(request,id=-1):
    if request.POST:
        form = CreateDirForm(data=request.POST)
        if form.is_valid():
           dir=form.save()   
           return redirect('/vdir/detail/'+str(dir.id))
        print('invalid')
        context = {}
        context.update(csrf(request))
        context['form']=form
    else:
        if id != -1:
            form = CreateDirForm(initial = {'parent':id},pid=id)
        else:
            form = CreateDirForm()
        context = {}
        context.update(csrf(request))
        context['form']=form
    return render_to_response('vdir/create.html',context)

def rename(request,id=0):
    if request.POST:
        dir = get_object_or_404(VDir,pk=id)
        form=RenameDirForm(data=request.POST,instance=dir)

        if form.is_valid():
            form.save()
            return redirect('/vdir/detail/'+str(id))

        print(id)
        context = {}
        context['form']=form
        context['dir'] = dir
        context.update(csrf(request))            
    else:
        dir = get_object_or_404(VDir,pk=id)
        form = RenameDirForm()
        context = {}
        context['form']=form
        context['dir'] = dir
        context.update(csrf(request))
    return render_to_response('vdir/rename.html',context)

@login_required
def delete(request,id):
    vdir = get_object_or_404(VDir,pk=id)

    from django.conf import settings

    realpath = settings.MEDIA_ROOT+vdir.get_full_path()
    import os
    if os.path.exists(realpath):
        import shutil 
        shutil.rmtree(realpath)

    vdir.delete()
    if vdir.parent:
        return redirect('/vdir/detail/'+str(vdir.parent.id))
    else:
        return redirect('/vdir/detail/')

@login_required
def dir(request,id=0):
    context={}
    if id<=0:
        dir = None
        subdirs = VDir.objects.filter(parent=None)
    else:
        dir = get_object_or_404(VDir,pk=id)
        subdirs = dir.vdir_set.all()

    context['dir']=dir
    context['subdirs']=subdirs
    paginator = Paginator(subdirs, 20) # Show 20 contacts per page
    page = request.GET.get('page')
    try:
        subdirs = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        subdirs = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        subdirs = paginator.page(paginator.num_pages)
    context['subdirs']=subdirs
    start = (subdirs.number)/5*5+1
    end =   min(start+5,paginator.num_pages)
    
    context['somevar'] = 1
    context['pages'] = range(start,end+1)
    return render_to_response('vdir/detail.html',context)

